package com.mall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.entity.Product;
import com.mall.httpResult.HttpResult;
import com.mall.httpResult.ResultCodeEnum;
import com.mall.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    @ApiOperation("首页显示商品信息,可分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int")
    })
    public HttpResult listProductByPage(
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        List<Product> list = productService.listProduct(pageNo, pageSize);
        if (list != null) {
            return HttpResult.success(list);
        }
        return HttpResult.failure(ResultCodeEnum.NOT_FOUND, "暂时没有商品显示");
    }

    @GetMapping("/search")
    @ApiOperation("搜索商品，可分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字", required = true, dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int")
    })
    public HttpResult listProductByPage(
            @RequestParam(value = "keyword") String keyword,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        List<Product> list = productService.findByKeyword(keyword, pageNo, pageSize);
        if (list != null) {
            return HttpResult.success(list);
        }
        return HttpResult.failure(ResultCodeEnum.NOT_FOUND, "没有你想要搜索的商品");
    }

    @GetMapping("/sort")
    @ApiOperation("首页查看分类商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "类型Id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int")
    })
    public HttpResult listProductByPage(
            @RequestParam(value = "typeId") int typeId,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        List<Product> list = productService.listProductByType(typeId, pageNo, pageSize);
        if (list != null) {
            return HttpResult.success(list);
        }
        return HttpResult.failure(ResultCodeEnum.NOT_FOUND, "改分类没有商品");
    }

    @GetMapping("/sortByType")
    @ApiOperation("按照销量、价格、新品来排序商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sortType", value = "排序类型,综合0、销量1、新品2、价格升序3、价格降序4", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageNo", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示数量", dataType = "int")
    })
    public HttpResult sortByType(
            @RequestParam(value = "sortType") int sortType,
            @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        return HttpResult.success(productService.sortByType(sortType, pageNo, pageSize));

    }

    @PostMapping("/updateProduct")
    @ApiOperation("修改商品信息")
    public HttpResult updateProduct(@RequestBody Product product) {
    
        try {
            productService.updateById(product);
        } catch (Exception e) {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR);
        }
        return HttpResult.success();
    }

    @PostMapping("/addProduct")
    @ApiOperation("上架商品")
    public HttpResult addProduct(@RequestBody Product product) {

        int result = productService.addProduct(product);
        if (result == 1) {
            return HttpResult.success();
        } else {
            return HttpResult.failure(ResultCodeEnum.SERVER_ERROR);
        }
    }
    
    @GetMapping("/findProductById")
    @ApiOperation("根据Id查找商品")
    public HttpResult findProductById(@RequestParam int id) {
        
        Product product = productService.selectById(id);
        
        if (product == null) {
            return HttpResult.failure(ResultCodeEnum.NOT_FOUND);
        }
        return HttpResult.success(product);
    }
}

